package cn.xgx.web.controller.backstage;

import cn.xgx.result.ResponseData;
import cn.xgx.service.UserService;
import cn.xgx.web.model.input.LoginForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api")
public class BackstageIndexController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseData login(@RequestBody LoginForm loginForm){
        //生成token并存储
        return userService.login(loginForm.getUserName(),loginForm.getPassword());
    }

    @DeleteMapping("/logout")
    public ResponseData logout(HttpServletRequest request){
        //读取token的值
        String token = request.getHeader("Authorization");
        //移除token
//        userService.removeToken(token);
        return  userService.removeToken(token);
    }

    @GetMapping("/session")
    public ResponseData session(HttpServletRequest request){
        //读取token的值
        String token = request.getHeader("Authorization");
        return ResponseData.success(userService.getLoginUser(token));
    }

}
